<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/css/my_log_register.css">
</head>
<body >
    <!-- 注册提示框 -->
    <div class="col">
        <div class="alert alert-danger alert-dismissible fade show invisible text-center" style="width: 42rem;"
            role="alert">
            <strong>提示：</strong><label for="" id="msg">555</label>
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
    </div>

    <div id="container" class="shadow p-3 mb-5 bg-white rounded">
        <div id="title">
            <h1 style="text-align: center; padding-top: 1rem;">注册</h1>
        </div>

        <form id="registerForm" action="/registerDone" method="POST">
            <!-- 文本框 -->
            <div id="body_middle">
                <div class="row">
                  <div class="col">
                      <center>
                          <input type="text" class="form-control" id="username" name="username" placeholder="用户名/手机号/邮箱" style="width: 50%; text-align: center;">
                      </center>
                  </div>
                </div>
                <div class="row">
                    <div class="col">
                        <center>
                            <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码" style="width: 50%; text-align: center">
                        </center>
                    </div>
                </div>
                <div class="row">
                    <div class="col">
                        <center>
                            <input type="password" class="form-control" id="confirmpassword" name="confirmpassword" placeholder="请确认密码" style="width: 50%; text-align: center">
                        </center>
                    </div>
                </div>
            </div>
            <br>
                <!-- 按钮组件 -->
                <div style="text-align: center;">
                    <button type="button" class="btn btn-outline-success" style="width: 6rem; margin-right: 2rem;" onclick="jump()">注册</button> 
                    <!-- <button type="button" id="reset" class="btn btn-outline-success"  style="width: 6rem;" name="reset" >重置</button>  -->
                    <button type="button" class="btn btn-outline-success" style="width: 6rem;" name="reset" onclick="jumpOut()">取消</button>
                </div>
            </div>
        </form>
    </div>
    
    <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
    <script src="../static/js/jquery-3.5.1.min.js"></script>
    <script src="../static/js/bootstrap.min.js"></script>
    <script src="../static/js/register_btn.js"></script>
    <script src="../static/js/register_input_focus.js"></script>

    <script>
        // 点击注册时 执行的方法（获取数据，进行初步验证，验证通过则通过ajax向后台提交数据；否则提示验证不通过的信息）
        function jumpOut(){
            window.location.href="/login"
        }
        function jump() {
            let username = $('#username').val();
            let password = $('#password').val();
            let confirmpassword = $('#confirmpassword').val();
            if(username.length>0 && password.length>0 && password===confirmpassword){
                axios({
                    method:"post",
                    url:"/registerDone",
                    data:$("#registerForm").serialize()
                }).then(function(res){
                    // axios工具下，服务器真正返回的数据在返回对象的data中
                    res=res.data;
                    if(res.code===200){
                        window.location.href="/login";
                    }else{
                        alert_fn(res.msg)
                    }
                }).catch((err)=>{
                    alert_fn(err),
                    console.log(err);
                })
            }else{
                alert_fn("用户名、密码不能为空，并且两次密码需一致！！！")
            }
        }
        // 这个方法包装了alert的相关行为和显示的内容
        function alert_fn(msg) {
            msg=msg || '密码不能为空，两次密码应该一致';
            // 替换提示的内容
            $('#msg').text(msg);
            // 移除不显示的class，则alert将显示
            $('.alert').removeClass('invisible');
            // 三秒中后，给alert增加不显示的class，则alert重新隐藏
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 3000);
        }
    </script>

</body>
</html>